package br.inf.solutions.apresentacoes.ssp.view.composer;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Textbox;

import br.com.vexillum.util.HibernateUtils;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.ParametrizationRemovalController;
import br.inf.solutions.apresentacoes.ssp.control.RemovalController;
import br.inf.solutions.apresentacoes.ssp.model.ParametrizationRemoval;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.basics.SpeciesRemoval;
import br.inf.solutions.apresentacoes.ssp.model.bulletin.items.Removal;

@SuppressWarnings({ "serial" })
@org.springframework.stereotype.Component
@Scope("prototype")
public class RemovalComposer  extends BulletinItemComposer<Removal, RemovalController> {

	private ParametrizationRemoval presentParametrization;
	
	public ParametrizationRemoval getPresentParametrization() {
		return presentParametrization;
	}

	public void setPresentParametrization(
			ParametrizationRemoval presentParametrization) {
		this.presentParametrization = presentParametrization;
	}
	
	@Override
	public void clearForm() {
		super.clearForm();
		setPresentParametrization(null);
		((Textbox) getComponentById("fldLaw")).setRawValue(null);
	}

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		if(getRetification()){
			searchPresentParametrization();
		}
		loadBinder();
	}
	
	public void searchPresentParametrization(){
		Return ret = getControl().getPresentParametrization();
		if(ret.isValid() && ret.getList() != null && !ret.getList().isEmpty()){
			setPresentParametrization((ParametrizationRemoval) ret.getList().get(0));
		}
		loadBinder();
	}
	
	public void searchNotPublicatedRemoval(){
		if(getEntity().getPerson() != null && getEntity().getPerson().getRg() != null && getEntity().getSpecies() != null){
			Return ret = getControl().doAction("getNotPublicatedRemoval");
			if(ret.getList() != null && !ret.getList().isEmpty()){
				setEntity((Removal) ret.getList().get(0));
				setEntity((Removal) getEntity().cloneEntity());
			} else {
				Person person = getEntity().getPerson();
				SpeciesRemoval specie = getEntity().getSpecies();
				
				setEntity(getEntityObject());
				
				getEntity().setPerson(person);
				getEntity().setSpecies(specie);
			}
			loadBinder();
		}
	}
	
	@Override
	protected String getUpdatePage() {
		return "/paginas/afastamento/modalCadastrar.zul";
	}

	@Override
	public RemovalController getControl() {
		return SpringFactory.getController("removalController", RemovalController.class, ReflectionUtils.prepareDataForPersistence(this));
	}
	
	@SuppressWarnings("rawtypes")
	public EventListener getEventPersonNotFound(){
		return new EventListener<Event>() {

			@Override
			public void onEvent(Event event) throws Exception {
				if (event.getName().equals("onOK")) {
					Executions.sendRedirect("/paginas/pessoas/cadastrar.zul");
				} else {
					getEntity().setPerson(new Person());
					loadBinder();
				}
				
			}
		};
	}
	
	@SuppressWarnings("unchecked")
	public List<SpeciesRemoval> getSpecies(){
		ParametrizationRemovalController controlador = SpringFactory.getController("parametrizationRemovalController", ParametrizationRemovalController.class, null);
		List<ParametrizationRemoval> listParam = (List<ParametrizationRemoval>) controlador.doAction("listAllActivated").getList();
		
		List<SpeciesRemoval> list = new ArrayList<>();
		for(ParametrizationRemoval param : listParam){
			list.add(HibernateUtils.materializeProxy(param.getSpecies()));
		}
		return list;
	}

	@Override
	public Removal getEntityObject() {
		return new Removal();
	}

	@Override
	protected String getDeactivationMessage() {
		return null;
	}
	
	@Override
	public Return searchByHQL() {
		if(getEntity().getPerson() == null){
			getEntity().setPerson(new Person());
		}
		getEntity().getPerson().setRg(getRg());
		return super.searchByHQL();
	}

}
